<?php
/**
 * 美食TAG管理
 *
 * @copyright dongping
 * @since 2012-03-24
 */


include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();



function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	
	// 入库、未入库
	cgi::both($g_cgival["type"], "type", "");
	if ($g_cgival["type"] != "in" and $g_cgival["type"] != "no_in")
	{
		$g_cgival["type"] = "no_in";
	}
	
	// obj_type 未入库时TAG属于人、美食分享、专辑、餐厅？
	cgi::both($g_cgival["obj_type"], "obj_type", "");
	if ($g_cgival["obj_type"] != "people" 
			and $g_cgival["obj_type"] != "share" 
			and $g_cgival["obj_type"] != "album" 
			and $g_cgival["obj_type"] != "res")
	{
		$g_cgival["obj_type"] = "people";
	}
	
	// 搜索词
	cgi::both($g_cgival["tag"], "tag", "");
	$g_cgival["tag"] = string::un_script_code($g_cgival["tag"]);
	$g_cgival["tag"] = string::un_html($g_cgival["tag"]);
	
	
	// 分类
	cgi::both($g_cgival["tag_class"], "tag_class", "");
	$g_cgival["tag_class"] = string::un_script_code($g_cgival["tag_class"]);
	$g_cgival["tag_class"] = string::un_html($g_cgival["tag_class"]);
	
	// order
	cgi::both($g_cgival["order"], "order",  "");
	$g_cgival["order"] = string::un_script_code($g_cgival["order"]);
	$g_cgival["order"] = string::un_html($g_cgival["order"]);
	
	
	// 分页
	cgi::both($g_cgival["p"], "p", 1);
	cgi::both($g_cgival["pcount"], "pcount", 40);
	$g_cgival["p"] = intval($g_cgival["p"]);
	$g_cgival["pcount"] = intval($g_cgival["pcount"]);
}

function get_data()
{
	global $g_cgival, $g_pro, $g_show;
	
	$g_show = $g_cgival;
	$type = $g_cgival["type"];
	$obj_type = $g_cgival["obj_type"];
	$tag = $g_cgival["tag"];
	$tag_class = $g_cgival["tag_class"];
	$order = $g_cgival["order"];
	
	// 拿到当前站点的site_id
	$site_id = admin::admin_get_user_site_id();	
	
	// DB类
	$db_food_tag = new db_food_tag();
	
	
	// 末入库
	if ($type == "no_in")
	{
		if ($obj_type == "people")
		{
			$db = new db_food_user_tag();
			$db->debug = true;
			
			$data = $db->admin_search_tags($site_id, $tag, $order, $g_cgival["p"], $g_cgival["pcount"]);
			$count = $db->admin_search_tags_count($site_id, $tag);
		}
		else if ($obj_type == "share")
		{
			$db = new db_food_status_tag();
			$db->debug = true;
			
			$data = $db->admin_search_tags($site_id, $tag, $order, $g_cgival["p"], $g_cgival["pcount"]);
			$count = $db->admin_search_tags_count($site_id, $tag);
		}
		else if ($obj_type == "album")
		{
			$db = new db_food_album_tag();
			$db->debug = true;
				
			$data = $db->admin_search_tags($site_id, $tag, $order, $g_cgival["p"], $g_cgival["pcount"]);
			$count = $db->admin_search_tags_count($site_id, $tag);
		}
		else if ($obj_type == "res")
		{
			$db = new db_web_rstrt_tag();
			$db->debug = true;
				
			$data = $db->admin_search_tags($site_id, $tag, $order, $g_cgival["p"], $g_cgival["pcount"]);
			$count = $db->admin_search_tags_count($site_id, $tag);
		}
				
		
		// 处理所有查到的TAG，程序判断是否已经入库
		if (is_array($data) and count($data) > 0)
		{
			$tag_ary = array();
			$food_tag_filter = array();
			foreach($data as $k=>$v)
			{
				$tag_ary[] = $v["tag"];
			}
			
			$data_food_tag = $db_food_tag->admin_get_list_by_tags_ary($site_id, $tag_ary);
			if (is_array($data_food_tag))
			{
				foreach($data_food_tag as $k=>$v)
				{
					$food_tag_filter[$v["tag_name"]] = 1;
				}
			}
			
			
			foreach($data as $k => $v)
			{
				if (array_key_exists($v["tag"], $food_tag_filter))
				{
					$data[$k]["in"] = 1;
				}
				else
				{
					$data[$k]["in"] = 0;
				}
			}
			
			$g_show["data"] = $data;
			$g_show["count"] = $count;
		}
		else
		{
			$g_show["data_none"] = 1;
		}
		
				
		// Pager
		$max_page = page_bar::get_page_max($g_show["count"], $g_cgival['pcount'], $g_cgival["p"]);
		
		$page_bar_style = array(
				"curt"		=> $g_cgival["p"],
				"max"		=> $max_page,
				"key"		=> "p",
		);
		
		$g_show["page_bar"] = "<b>{$g_show["count"]}</b> 条记录&nbsp;&nbsp;第{$g_cgival["p"]}页&nbsp;共{$max_page}页&nbsp;" . page_bar::bar2($page_bar_style);
		
				
		
		
	}
	else if ($type == "in")
	{
		$db = new db_food_tag();
		$db->debug = true;
		
		if ($tag_class == "全部")
		{
			$tag_class = "";
		}
		
		$data = $db->admin_tag_search($site_id, $tag_class, $tag, $order, $g_cgival["p"], $g_cgival["pcount"]);
		$count = $db->admin_tag_search_count($site_id, $tag_class, $tag);
		
		if (count($data) > 0)
		{
			$g_show["data"] = $data;
			$g_show["count"] = $count;
		}
		else
		{
			$g_show["data_none"] = 1;
		}
				
		// Pager
		$max_page = page_bar::get_page_max($g_show["count"], $g_cgival['pcount'], $g_cgival["p"]);
		
		$page_bar_style = array(
				"curt"		=> $g_cgival["p"],
				"max"		=> $max_page,
				"key"		=> "p",
		);
		
		$g_show["page_bar"] = "<b>{$g_show["count"]}</b> 条记录&nbsp;&nbsp;第{$g_cgival["p"]}页&nbsp;共{$max_page}页&nbsp;" . page_bar::bar2($page_bar_style);
		
		
	}

	
	
	
	
	
	
}

try 
{
	admin_check_login();
	// 	admin_check_user_priv("foodv2/main_nav.php");	
	
	check_cgi_pro();
	get_data();
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


function show_pro()
{
	global $g_cgival, $g_pro, $g_show;
	
	if (isset($_GET["debug"]))
	{
		echo "<pre>";
		print_r($g_show);
	}
	
	global $g_show;
	$smarty = p_get_smarty ();
	$smarty->assign ( 'g_show', $g_show );
	$smarty->display ( "foodv2/tag/tag_index.html" );
}